package com.tsingteng.cosfun.ui.cosfun;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.CoordinatorLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.widget.dialog.BaseBottomSheetFragment;
import com.umeng.socialize.bean.SHARE_MEDIA;

public class ShareFragment extends BaseBottomSheetFragment implements View.OnClickListener {
    private static final String ARG_ITEM_COUNT = "item_count";
    OnShareItemClickLisenter onShareItemClickLisenter = null;
    private String type = null;
    private boolean mHomeCopy =false;
    // TODO: Customize parameter argument names

    // TODO: Customize parameters
    public static ShareFragment newInstance() {
        final ShareFragment fragment = new ShareFragment();
        final Bundle args = new Bundle();
        args.putInt(ARG_ITEM_COUNT, 20);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 在这里将view的高度设置为精确高度，即可屏蔽向上滑动不占全屏的手势。
        View view = inflater.inflate(R.layout.fragment_share_dialog, container, false);
        return view;
    }

    public void setLisenter(OnShareItemClickLisenter onShareItemClickLisenter) {
        this.onShareItemClickLisenter = onShareItemClickLisenter;
    }

    public void setType(String type) {
        this.type = type;
    }
    public void setHomeCopy(boolean homeCopy){
        this.mHomeCopy = homeCopy;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        LinearLayout weixin_tv = view.findViewById(R.id.share_weixin);
        weixin_tv.setOnClickListener(this);
        LinearLayout pyq_tv = view.findViewById(R.id.share_pengyouquan);
        pyq_tv.setOnClickListener(this);
        LinearLayout qq_tv = view.findViewById(R.id.share_qq);
        qq_tv.setOnClickListener(this);
        LinearLayout kj_tv = view.findViewById(R.id.share_kongjian);
        kj_tv.setOnClickListener(this);
        LinearLayout weibo_tv = view.findViewById(R.id.share_weibo);
        weibo_tv.setOnClickListener(this);
        LinearLayout fuzhi_tv = view.findViewById(R.id.share_fuzhi);
        fuzhi_tv.setOnClickListener(this);
        LinearLayout mSaveShare = view.findViewById(R.id.share_baocun);
        LinearLayout del = view.findViewById(R.id.share_del);
        LinearLayout mCopyll = view.findViewById(R.id.copy_ll);
        del.setOnClickListener(this);
        mSaveShare.setOnClickListener(this);
        LinearLayout nolike_tv = view.findViewById(R.id.share_buganxingqu);
        nolike_tv.setOnClickListener(this);
        LinearLayout jubao_tv = view.findViewById(R.id.share_jubao);
        jubao_tv.setOnClickListener(this);

        view.findViewById(R.id.share_cancel).setOnClickListener(this);
        if (!TextUtils.isEmpty(type) && TextUtils.equals(type, "play")) {
            mSaveShare.setVisibility(View.GONE);
            nolike_tv.setVisibility(View.INVISIBLE);
            jubao_tv.setVisibility(View.INVISIBLE);
        }else {
            nolike_tv.setVisibility(View.VISIBLE);
            jubao_tv.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(type) && TextUtils.equals(type, "del")) {
            del.setVisibility(View.VISIBLE);
        } else {
            del.setVisibility(View.INVISIBLE);
        }
        if (mHomeCopy){
            mCopyll.setVisibility(View.GONE);
        }else {
            mCopyll.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onStart() {
        super.onStart();
        if (null ==getActivity()){
            return;
        }
        if (mHomeCopy){
            topOffset = AppUtils.getScreenWidthAndHeight(getActivity())[1] / 7 * 5;
        }else {
            topOffset = AppUtils.getScreenWidthAndHeight(getActivity())[1] / 10 * 5;
        }
        // 设置软键盘不自动弹出
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
        FrameLayout bottomSheet = dialog.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet);
        if (bottomSheet != null) {
            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomSheet.getLayoutParams();
            layoutParams.height = getHeight();
            behavior = BottomSheetBehavior.from(bottomSheet);
            // 初始为展开状态
            behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.share_weixin:
                if (onShareItemClickLisenter != null)
                    onShareItemClickLisenter.share(SHARE_MEDIA.WEIXIN, 0);
                //  Toast.makeText(getActivity(), "微信", Toast.LENGTH_SHORT).show();
                dismiss();
                break;
            case R.id.share_pengyouquan:
                if (onShareItemClickLisenter != null)
                    onShareItemClickLisenter.share(SHARE_MEDIA.WEIXIN_CIRCLE, 0);
                //  Toast.makeText(getActivity(), "朋友圈", Toast.LENGTH_SHORT).show();
                dismiss();
                break;
            case R.id.share_qq:
                if (onShareItemClickLisenter != null)
                    onShareItemClickLisenter.share(SHARE_MEDIA.QQ, 0);
                //Toast.makeText(getActivity(), "QQ", Toast.LENGTH_SHORT).show();
                dismiss();
                break;
            case R.id.share_kongjian:
                if (onShareItemClickLisenter != null)
                    onShareItemClickLisenter.share(SHARE_MEDIA.QZONE, 0);
                // Toast.makeText(getActivity(), "QQ空间", Toast.LENGTH_SHORT).show();
                dismiss();
                break;
            case R.id.share_weibo:
                if (onShareItemClickLisenter != null)
                    onShareItemClickLisenter.share(SHARE_MEDIA.SINA, 0);
                //Toast.makeText(getActivity(), "微博", Toast.LENGTH_SHORT).show();
                dismiss();
                break;
            case R.id.share_fuzhi:
                if (onShareItemClickLisenter != null)
                    onShareItemClickLisenter.other(1);
                //Toast.makeText(getActivity(), "复制链接", Toast.LENGTH_SHORT).show();
                dismiss();
                break;
            case R.id.share_baocun:
                if (onShareItemClickLisenter != null)
                    onShareItemClickLisenter.other(2);
                //Toast.makeText(getActivity(), "保存本地", Toast.LENGTH_SHORT).show();
                dismiss();
                break;
            case R.id.share_buganxingqu:
                if (onShareItemClickLisenter != null)
                    onShareItemClickLisenter.other(3);
                dismiss();
                break;
            case R.id.share_jubao:
                if (onShareItemClickLisenter != null) {
                    if (LogonUtils.chckLogon(getActivity(), false, 0)) {
                        onShareItemClickLisenter.other(4);
                    }
                }
                dismiss();
                break;
            case R.id.share_del:
                if (onShareItemClickLisenter != null) {
                    onShareItemClickLisenter.other(5);
                }
                dismiss();
                break;
            case R.id.share_cancel:
                dismiss();
                break;
        }
    }

    public interface OnShareItemClickLisenter {

        void share(SHARE_MEDIA share_media, int type);

        void other(int type);//1复制链接 2下载 3 不感兴趣 4 举报

    }

    public void setTag(String type) {
        this.type = type;
    }
}
